<?php require_once(dirname(__FILE__) . '/../include/config.inc.php');
require_once "function.php";
exit;

if (isset($uid)) {

    //有session 从数据库中读取用户名信息
    //$openid_str=$_SESSION['openid'];
    $sql = "SELECT u.*,g.groupname  as groupname  FROM `#@__user` as u 
				   left join `#@__usergroup` as g  on u.level=g.id  
				   WHERE u.id='$uid' ";
    $row = $dosql->GetOne($sql);
    if (is_array($row) && isset($row['id'])) {
        // 返回用户 id
        $id = $row['id'];
        $_SESSION['id'] = $id;
        $userInfo = $row;
        $level = intval($row['level']);
        $fee = intval($row['fee']);
    }
}

if ($level > 5) {
    exit();
}


// 上月 月奖励
$last_mouth_time1 = strtotime(date('Y-m-01', strtotime('-1 month')));
$last_mouth_time2 = strtotime(date('Y-m-t', strtotime('-1 month'))) + 86400;
// 当月 月奖励
$this_mouth_time1 = strtotime(date('Y-m-01'));
// 当日 奖励
$day_time1 = strtotime(date('Y-m-d'));


// 上月奖励
$last_mouth_time1 = strtotime(date('Y-m-01', strtotime('-1 month')));
$last_mouth_time2 = strtotime(date('Y-m-t', strtotime('-1 month'))) + 86400;
$sql_order = "SELECT `id`,`op`,`fee` FROM `#@__fee_log` WHERE `manageid`='$id' and op_time>$last_mouth_time1 and op_time<$last_mouth_time2  ";

//获取所有的 记录合计
$last_mouth_fee = 0;
$dosql->Execute($sql_order, 7);
while ($order = $dosql->GetArray(7)) {
    if ($order['op'] == 'add') {
        $last_mouth_fee += $order['fee'];
    } elseif ($order['op'] == 'move') {
        $last_mouth_fee -= $order['fee'];
    }
}

// 当月月奖励
$this_mouth_time1 = strtotime(date('Y-m-01'));
$sql_order2 = "SELECT `id`,`op`,`fee` FROM `#@__fee_log` WHERE `manageid`='$id' and op_time>$this_mouth_time1  ";

//获取所有的 记录合计
$this_mouth_fee = 0;
$dosql->Execute($sql_order2, 8);
while ($order2 = $dosql->GetArray(8)) {
    if ($order2['op'] == 'add') {
        $this_mouth_fee += $order2['fee'];
    } elseif ($order2['op'] == 'move') {
        $this_mouth_fee -= $order2['fee'];
    }
}

// 当日 奖励
$day_time1 = strtotime(date('Y-m-d'));
$sql_order3 = "SELECT `id`,`op`,`fee` FROM `#@__fee_log` WHERE `manageid`='$id' and op_time>$day_time1  ";

//获取所有的 记录合计
$day_fee = 0;
$dosql->Execute($sql_order3, 9);
while ($order33 = $dosql->GetArray(9)) {
    if ($order33['op'] == 'add') {
        $day_fee += $order33['fee'];
    } elseif ($order2['op'] == 'move') {
        $day_fee -= $order33['fee'];
    }
}

$type = isset($_GET['type']) ? htmlspecialchars($_GET['type']) : "";

switch ($type) {
    case "all":
        $order_title = "所有奖励";
        $sql_add = "";
        break;
    case "lastmonth":
        $order_title = " 上月奖励";
        $sql_add = " and op_time>$last_mouth_time1 and op_time<$last_mouth_time2 ";
        break;
    case "month":
        $order_title = "当月奖励";
        $sql_add = "  and op_time>$this_mouth_time1 ";
        break;
    case "today":
        $order_title = "当天奖励";
        $sql_add = "  and op_time>$day_time1 ";
        break;
    default:
        $order_title = "全部";
        $sql_add = "";
}

?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>奖励列表 <?php echo $cfg_webname; ?> </title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="description" content="<?php echo $cfg_webname; ?>">
        <link rel="stylesheet" href="lib/weui.min.css">
        <link rel="stylesheet" href="css/jquery-weui.css">
        <link rel="stylesheet" href="css/font-awesome.min.css">
        <link rel="stylesheet" href="css/demos.css?v=20201211.2116">
    </head>

<body ontouchstart>
<style>
    .weui-form-preview:after {
        border-bottom: none;
    }
</style>
<div class="top">
    <div class="user_top">
        <i>
            <?php
            if (intval($row['level']) > 1) {
                echo '<a href="manage.php">' . $userInfo['groupname'] . '</a>';
            } else {
                echo $userInfo['groupname'];
            } ?>
            UID:<?php echo $id; ?>
        </i>
        <div class="user_top_conn"><img src="<?php echo $userInfo['headimgurl'] ?>"/>
            <p><?php echo $userInfo['truename'] ?></p>
            <?php if (intval($row['level']) < 5) { ?><a class="user_link" href="member.php">返回用户</a><?php } ?>
        </div>
    </div>
</div>
<!-- 任务概况 -->
<div class="main" style="margin-top: 0">
    <div class="aui-palace aui-palace-one">
        <a href="fee.php?type=all" class="aui-palace-grid">
            <div class="aui-palace-grid-text">
                <h4><?php echo $fee; ?></h4>
                <h5>总奖励</h5>
            </div>
        </a>
        <a href="fee.php?type=lastmonth" class="aui-palace-grid">
            <div class="aui-palace-grid-text">
                <h4><?php echo $last_mouth_fee ?></h4>
                <h5 style="color: #985f0d ">上月</h5>
            </div>
        </a>
        <a href="fee.php?type=month" class="aui-palace-grid">
            <div class="aui-palace-grid-text">
                <h4><?php echo $this_mouth_fee ?></h4>
                <h5 style="color: #007aff ">本月</h5>
            </div>
        </a>
        <a href="fee.php?type=today" class="aui-palace-grid">
            <div class="aui-palace-grid-text">
                <h4 class="no-border"><?php echo $day_fee ?></h4>
                <h5 class="no-border" style="color: #0BB20C">当日</h5>
            </div>
        </a>
    </div>
</div>


<!-- 报修信息 -->
<div class="main">
    <div class="weui-cells__title"><i class="fa fa-id-card-o" aria-hidden="true"></i><?php echo $order_title; ?> 奖励列表
    </div>
    <!--工单列表开始-->
    <?php

    $sql = "SELECT * FROM `#@__fee_log`    WHERE   manageid='$uid'  ";
    if ($type != "") {
        $sql = $sql . $sql_add;
    }
    $sql .= "ORDER BY id DESC";

    $dopage->GetPage($sql, 8);
    while ($row = $dosql->GetArray()) {
        /*
        进度：
        0 已经撤单
        1 报修成功
        2 客服已经联系
        3 系统已经派单
        4 工程师接单
        5 维修完成
        6 已经评价
        */
        $orderid = $row['orderid'];

        $typei_fee = $row['type'];

        $typename = "工单";
        $op_str = '<a class=weui-form-preview__btn weui-form-preview__btn_primary" href="morderDetail.php?orderid=' . $row['orderid'] . '" ><i class="fa fa-navicon" aria-hidden="true"></i>工单详情</a>';
        $db_table = '#@__order';
        if ($typei_fee == "project") {
            $typename = "工程";
            $op_str = '<a class=weui-form-preview__btn weui-form-preview__btn_primary" href="projectDetail.php?projectid=' . $row['orderid'] . '" ><i class="fa fa-navicon" aria-hidden="true"></i>工程详情</a>';
            $db_table = '#@__project';
        }

        $item_info = $dosql->GetOne("select * from $db_table where id='$orderid' ");

        ?>

        <!--工单详情开始-->
        <div class="weui-form-preview">
            <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">编号：</label>
                    <span class="weui-form-preview__value">
        <?php echo $typename . " " . date("Ymd", $item_info['post_time']) . "-" . $item_info['id'] ?>
        </span>
                </div>
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">单位：</label>
                    <span class="weui-form-preview__value"><?php echo $item_info['school'] ?></span></div>

                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">联系人：</label>
                    <span class="weui-form-preview__value"><?php echo $item_info['truename'] ?></span></div>

                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">电话：</label>
                    <span class="weui-form-preview__value"><a href="tel:<?php echo $item_info['mobile'] ?>"><i class="fa fa-mobile"><?php echo $item_info['mobile'] ?></a></i></span>
                </div>


                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">地址：</label>
                    <span class="weui-form-preview__value"><i class="fa fa-map-marker"><?php echo $item_info['address'] ?></i></span>
                </div>

                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">任务类型：</label>
                    <span class="weui-form-preview__value"><i class="fa  fa-print red"></i> <?php echo getRepairMode($item_info['apply_device']) ?></span>
                </div>
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">故障描述：</label>
                    <span class="weui-form-preview__value"><?php echo $item_info['content'] ?></span></div>

                <?php if (!empty($row['manageid'])) { ?>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">执行人：</label>
                        <span class="weui-form-preview__value">
			<?php
            if (!empty($item_info['manageid'])) {
                $m_str = $item_info['manageid'];
                $m_arr = explode(",", $m_str);
                foreach ($m_arr as $key => $val) {
                    echo ' <i class="fa  fa-user-o red"></i>      ' . getManage($val);
                }
            }
            ?>
        
        </span></div>
                <?php } ?>

                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">任务时间：</label>
                    <span class="weui-form-preview__value"><i class="fa fa-play green"></i> <?php echo date("Y-m-d H:i:s", $item_info['begintime']) . "  
		<br/><i  class=' red fa  fa-stop'></i>     " . date("Y-m-d H:i:s", $item_info['endtime']) ?></span></div>
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">奖励情况：</label>
                    <span class="weui-form-preview__value">
                         <?php
                         $dosql->Execute("SELECT * FROM `#@__fee_log` WHERE orderid='$orderid'  ORDER BY id asc",99);
                         while ($row2_fee = $dosql->GetArray(99)) {
                             if ($row2_fee['op'] == "add") {
                                 echo '<li><i class="fa fa-plus red">增加</i><span class="blue"> ' . $row2_fee['fee'] . '</span>  ' . date("m-d H:i:s", $row2_fee['op_time']) . '</li>';

                             } else {
                                 echo '<li><i class="fa fa-minus green">减少</i><span class="blue"> ' . $row2_fee['fee'] . '</span>  ' . date("m-d H:i:s", $row2_fee['op_time']) . '</li>';

                             }

                         }

                         ?>
                    </span>
                </div>

            </div>
            <div class="weui-form-preview__ft op"> <?php echo $op_str ?> </div>
            <Div class="juchi"></Div>
        </div>
        <!--工单详情结束-->

    <?php } ?>

    <!--工单列表结束-->
</div>
<div class="page-div">

    <?php echo $dopage->GetList(); ?>

</div>
<div class="clear"></div>
<div class="sub_btn"></div>
<script src="lib/jquery-2.1.4.js"></script>
<script src="lib/fastclick.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script>
    $(function () {
        FastClick.attach(document.body);
    });

    function jiedan(id) {
        var manageid = $("#manageid").val();
        $.ajax({
            type: 'POST',
            url: "op.php",
            data: {id: id, op: 'jiedan', 'manageid': manageid},
            dataType: 'JSON',
            success: function (result) {
                console.log(result);
                if (result.errno == 0) {
                    console.log(result.msg);
                    if (result.msg === "ok") {
                        $.toast("接单成功", function () {
                            //window.location.reload();
                            window.location.href = "morder.php?jindu=4";
                        });

                    } else {
                        $.alert("接单失败，下手要快哦", "温馨提示");
                    }
                }
            }
        });

    }

    // 微信jssdk
    wx.config({
        debug: false,
        appId: '<?php echo $signPackage["appId"];?>',
        timestamp: <?php echo $signPackage["timestamp"];?>,
        nonceStr: '<?php echo $signPackage["nonceStr"];?>',
        signature: '<?php echo $signPackage["signature"];?>',
        jsApiList: [
            // 所有要调用的 API 都要加到这个列表中
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            'openAddress',
            'chooseImage',
            'previewImage',
            'uploadImage',
            'downloadImage',
            'openLocation',
            'getLocation'
        ]
    });
    wx.ready(function () {
        //打开导航
        $("#daohang").on("click", function () {
            var orderid = $(this).data("orderid");
            console.log(orderid);
            $.ajax({
                type: 'POST',
                url: "dingwei.php",
                data: {op: 'dingwei', 'id': orderid},
                dataType: 'JSON',
                success: function (result) {
                    console.log(result);
                    if (result.errno == 0) {
                        console.log(result.msg);
                        if (result.msg === "ok") {
                            wx.openLocation({
                                latitude: result.data.latitude, // 纬度，浮点数，范围为90 ~ -90
                                longitude: result.data.longitude, // 经度，浮点数，范围为180 ~ -180。
                                name: result.data.school, // 位置名
                                address: result.data.address, // 地址详情说明
                                scale: result.data.accuracy, // 地图缩放级别,整形值,范围从1~28。默认为最大

                            });
                        } else {
                            $.alert("获取位置失败，客户没有进行定位", "温馨提示");
                        }
                    }
                }
            });
        })

    })

</script>
<script src="js/jquery-weui.js"></script>
<?php require_once('footer.php'); ?>